package ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.subpanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControllSoftware;
import ch.zbw.pra.sysventory.ui.gui.panels.settingspanel.SettingsPanel;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;

public class SoftwareSpezPanel extends JPanel implements ActionListener, ChangeListener {

	private static final long serialVersionUID = 8636096882014725866L;
	private JCheckBox softwareName;
	private JCheckBox softwareVendor;
	private JCheckBox softwareVersion;
	private VisuControllSoftware visuControllSoftware;
	private int amountOfFlags;

	public SoftwareSpezPanel(VisuControllSoftware visuControllSoftware) {
		this.visuControllSoftware = visuControllSoftware;
		this.amountOfFlags = 0;
		init();
	}

	public void init() {
		this.softwareName = new JCheckBox("Software-Name", this.visuControllSoftware.isSoftwareName());
		this.softwareName.addActionListener(this);
		this.softwareName.addChangeListener(this);
		this.softwareVendor = new JCheckBox("Software-Hersteller", this.visuControllSoftware.isSoftwareVendor());
		this.softwareVendor.addActionListener(this);
		this.softwareVendor.addChangeListener(this);
		this.softwareVersion = new JCheckBox("Software-Version", this.visuControllSoftware.isSoftwareVersion());
		this.softwareVersion.addActionListener(this);
		this.softwareVersion.addChangeListener(this);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(this);
		double checkBox = 0.0;
		double lable = 1.0;
		gridBagLayout.columnWeights = new double[] { checkBox, checkBox, checkBox, lable };
		int widthCheckbox = SettingsPanel.WIDTH_CHECKBOX;
		gridBagLayout.columnWidths = new int[] { widthCheckbox, widthCheckbox, widthCheckbox, 0 };
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.softwareName);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.softwareVendor);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, this.softwareVersion);
		GuiLayout.addToLayout(this, gridBagLayout, gridBagConstraints, x++, y, 1, 1, new JLabel());

	}

	public void setValueToAllCheckBoxes(boolean flag) {
		this.softwareName.setSelected(flag);
		this.softwareVendor.setSelected(flag);
		this.softwareVersion.setSelected(flag);
	}

	public void actionPerformed(ActionEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.softwareName) {
			this.visuControllSoftware.setSoftwareName(this.softwareName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.softwareVendor) {
			this.visuControllSoftware.setSoftwareVendor(this.softwareVendor.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.softwareVersion) {
			this.visuControllSoftware.setSoftwareVersion(this.softwareVersion.isSelected());
			checkCanCrement();
		}
	}

	public void stateChanged(ChangeEvent e) {
		this.amountOfFlags = DomainController.getInstance().getSettingsPanel().countAllSelectedFlags();
		if (e.getSource() == this.softwareName) {
			this.visuControllSoftware.setSoftwareName(this.softwareName.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.softwareVendor) {
			this.visuControllSoftware.setSoftwareVendor(this.softwareVendor.isSelected());
			checkCanCrement();
		} else if (e.getSource() == this.softwareVersion) {
			this.visuControllSoftware.setSoftwareVersion(this.softwareVersion.isSelected());
			checkCanCrement();
		}
	}

	public void checkCanCrement() {
		if (canCrement()) {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(true);
		} else {
			DomainController.getInstance().getSettingsPanel().setAllEnabledStates(false);
		}
	}

	public boolean canCrement() {
		return this.amountOfFlags < 8;
	}

	public boolean getSoftwareNameState() {
		return this.softwareName.isSelected();
	}

	public boolean getSoftwareVendorState() {
		return this.softwareVendor.isSelected();
	}

	public boolean getSoftwareVersionState() {
		return this.softwareVersion.isSelected();
	}

	public void setAllEnabledStates(boolean beEnabledFlag) {
		if (!this.softwareName.isSelected()) {
			this.softwareName.setEnabled(beEnabledFlag);
		}
		if (!this.softwareVendor.isSelected()) {
			this.softwareVendor.setEnabled(beEnabledFlag);
		}
		if (!this.softwareVersion.isSelected()) {
			this.softwareVersion.setEnabled(beEnabledFlag);
		}

	}
}
